package org.gwt.grin.client.commands;

import org.gwt.grin.client.Segment;
import org.gwt.grin.client.Show;

import com.google.gwt.core.client.GWT;

/**
 * A command to make a new segment be the active one within a show.
 * 
 * @author     Bill Foote (http://jovial.com)
 * @author ggeorg
 */
public class ActivateSegmentCommand extends Command {

	protected final boolean push;
	protected final boolean pop;
	protected Segment segment;

	public ActivateSegmentCommand(Show show) {
		this(show, false, false);
	}

	public ActivateSegmentCommand(Show show, boolean push, boolean pop) {
		super(show);
		this.push = push;
		this.pop = pop;
	}
	
	public void setup(Segment segment) {
		this.segment = segment;
	}

	public boolean isPush() {
		return push;
	}

	public boolean isPop() {
		return pop;
	}

	public Segment getSegment() {
		return segment;
	}

	public void execute() {
		if (push) {
			show.pushCurrentSegment();
		}
		Segment seg = segment;
		if (pop) {
			seg = show.popSegmentStack();

			if(GWT.isClient() && seg == null) {
				GWT.log("*** Error: Popping segment stack gets null ***");
			}
		}
		if (seg != null) {
			show.doActivateSegment(seg);
		}
	}

}
